/*********************************************************************
* File: Finances.cpp
*
* \brief Implements the finances API.
*
* \author Instituto Superior Tecnico de Lisboa, Portugal
* \author Technical University of Lisbon, Portugal
* 
* \author Agentes Autonomos e Sistemas Multiagente
* \author Autonomous Agents and Multiagent Systems
* 
* \version	1.0
* \date		2006/2007
*********************************************************************/
#include "Finances.h"
#include "../../util/util.h"
#include "Combat.h"
#include "../Agent.h"
using namespace agents;

/**
* \brief The class constructor.
*
* \param[in] the agent
**/
Finances::Finances(Agent &owner)
:_owner(owner)
{
	_money = 0;
}

/**
* \brief The class destructor.
**/
Finances::~Finances(){}

/**
* \brief Setups for the new round. 
* 
* Should be invoked when a new round starts.
**/
void Finances::newRound(){}

/**
* \brief Whether the agent is still in time to buy armory.
*
* \see agents::interfaces::IFinances::withinBuyTime
**/
bool Finances::withinBuyTime()
{
	return (g_world->getTimeRoundStarted() + CVAR_GET_FLOAT("mp_buytime") < _owner.getWorld().getTime());
}

/**
* \brief Buy item by name.
*
* \param[in] name The weapon name.
*
* \see agents::interfaces::IFinances::buy(char* name)
**/
bool Finances::buy(char* name){
	return buy(_owner.combat->weaponNameToId(name));
}
/**
* \brief Buy item by id.
*
* \param[in] item The item id.
*
* \see agents::interfaces::IFinances::buy(WeaponIds::Enumeration)
**/
bool Finances::buy(WeaponIds::Enumeration item)
{
	if(_owner.getTeam() == CounterStrikeTeams::counterTerrorist)
		return buyCounterTerrorist(item);
	else
		return buyTerrorist(item);
}
/**
* \brief Buy item by id for counter-terrorists.
*
* \param[in] item The item id.
**/
bool Finances::buyCounterTerrorist(WeaponIds::Enumeration item)
{
	if(!inBuyZone())
		return false;

	switch(item)
	{
	// Pistols
	case WeaponIds::glock18:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 1;menuselect 1");
		break;
	case WeaponIds::usp:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 1;menuselect 2");
		break;
	case WeaponIds::p228:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 1;menuselect 3");
		break;
	case WeaponIds::dEagle:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 1;menuselect 4");
		break;
	case WeaponIds::fiveSeven:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 1;menuselect 5");
		break;

	// Shotguns
	case WeaponIds::m3:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 2;menuselect 1");
		break;
	case WeaponIds::xm1014:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 2;menuselect 2");
		break;
	
	//Sub-Machine Guns	
	case WeaponIds::tmp:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 3;menuselect 1");
		break;
	case WeaponIds::mp5Navy:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 3;menuselect 2");
		break;
	case WeaponIds::ump45:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 3;menuselect 3");
		break;
	case WeaponIds::p90:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 3;menuselect 4");
		break;

	//Riffles
	case WeaponIds::famas:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 4;menuselect 1");
		break;
	case WeaponIds::scout:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 4;menuselect 2");
		break;
	case WeaponIds::m4A1:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 4;menuselect 3");
		break;
	case WeaponIds::aug:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 4;menuselect 4");
		break;
	case WeaponIds::sg550:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 4;menuselect 5");
		break;
	case WeaponIds::awp:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 4;menuselect 6");
		break;
	
	//Machine Guns
	case WeaponIds::m249:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 5;menuselect 1");
		break;
	
	//Ammo
	case WeaponIds::ammoPrimary:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 6");
		break;
	case WeaponIds::ammoSecondary:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 7");
		break;
	
	//Equipment
	case WeaponIds::vest:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 8;menuselect 1");
		break;
	case WeaponIds::vestHelm:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 8;menuselect 2");
		break;
	case WeaponIds::flashBang:
		util::fakeClientCommand(_owner.getEdict(), "buyequip;menuselect 3");
		break;
	case WeaponIds::heGrenade:
		util::fakeClientCommand(_owner.getEdict(), "buyequip;menuselect 4");
		break;
	case WeaponIds::smokeGrenade:
		util::fakeClientCommand(_owner.getEdict(), "buyequip;menuselect 5");
		break;
	case WeaponIds::defuser:
		util::fakeClientCommand(_owner.getEdict(), "buy;menuselect 8;menuselect 7");
		break;
	case WeaponIds::nightVisionGoggles:
		util::fakeClientCommand(_owner.getEdict(), "buyequip;menuselect 6");
		break;
	case WeaponIds::shieldGun:
		util::fakeClientCommand(_owner.getEdict(), "buyequip;menuselect 8");
		break;

	default:
		return false;
	}

	return true;
}
/**
* \brief Buy item by id for terrorists.
*
* \param[in] item The item id.
**/
bool Finances::buyTerrorist(WeaponIds::Enumeration item)
{
	if(!inBuyZone())
		return false;

	switch(item)
	{
	// Pistols
	case WeaponIds::glock18:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 1;menuselect 1");
		break;
	case WeaponIds::usp:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 1;menuselect 2");
		break;
	case WeaponIds::p228:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 1;menuselect 3");
		break;
	case WeaponIds::dEagle:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 1;menuselect 4");
		break;
	case WeaponIds::elite:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 1;menuselect 5");
		break;

	// Shotguns
	case WeaponIds::m3:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 2;menuselect 1");
		break;
	case WeaponIds::xm1014:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 2;menuselect 2");
		break;
	
	
	//Sub-Machine Guns
	case WeaponIds::mac10:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 3;menuselect 1");
		break;
	case WeaponIds::mp5Navy:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 3;menuselect 2");
		break;
	case WeaponIds::ump45:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 3;menuselect 3");
		break;
	case WeaponIds::p90:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 3;menuselect 4");
		break;

	//Riffles
	case WeaponIds::galil:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 4;menuselect 1");
		break;
	case WeaponIds::ak47:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 4;menuselect 2");
		break;
	case WeaponIds::scout:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 4;menuselect 3");
		break;
	case WeaponIds::sg552:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 4;menuselect 4");
		break;
	case WeaponIds::awp:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 4;menuselect 5");
		break;
	case WeaponIds::g3Sg1:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 4;menuselect 6");
		break;
	
	//Machine Guns
	case WeaponIds::m249:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 5;menuselect 1");
		break;
	
	//Ammo
	case WeaponIds::ammoPrimary:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 6");
		break;
	case WeaponIds::ammoSecondary:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 7");
		break;
	
	//Equipment
	case WeaponIds::vest:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 8;menuselect 1");
		break;
	case WeaponIds::vestHelm:
		util::fakeClientCommand(_owner.getEdict(),"buy;menuselect 8;menuselect 2");
		break;
	case WeaponIds::flashBang:
		util::fakeClientCommand(_owner.getEdict(), "buyequip;menuselect 3");
		break;
	case WeaponIds::heGrenade:
		util::fakeClientCommand(_owner.getEdict(), "buyequip;menuselect 4");
		break;
	case WeaponIds::smokeGrenade:
		util::fakeClientCommand(_owner.getEdict(), "buyequip;menuselect 5");
		break;
	case WeaponIds::nightVisionGoggles:
		util::fakeClientCommand(_owner.getEdict(), "buyequip;menuselect 6");
		break;

		//Unknown

	default:
		return false;
	}

	return true;
}